package com.jcl.recruit.user.service;

import com.jcl.core.common.page.PageResult;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.core.utils.EncodeMd;
import com.jcl.core.web.utils.RequestUtils;
import com.jcl.recruit.crawler.logic.CrawlerDownloadTaskLogic;
import com.jcl.recruit.user.logic.UserLogic;
import com.jcl.recruit.user.model.User;
import com.jcl.recruit.user.vo.UserSearchEntity;
import com.jcl.recruit.user.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 刘明新
 * @date 2018/8/2 下午7:11
 */
@Service
public class UserService {

    @Autowired
    private UserLogic userLogic;

    @Autowired
    private CrawlerDownloadTaskLogic crawlerDownloadTaskLogic;


    public PageResult pageList(UserSearchEntity entity, Integer curPage, Integer pageSize) {
        String corpCode = RequestUtils.getCorpCode();
        if (null == entity) {
            entity = new UserSearchEntity();
        }
        entity.setCorpCode(corpCode);
        PageResult result = userLogic.page(entity, curPage, pageSize);

        List<User> users = result.getData();

        if (CollectionUtils.isNotEmpty(users)) {
            List<UserVo> list = new ArrayList<>();
            List<Integer> userIds = new ArrayList<>();
            for (User user : users) {
                UserVo vo = new UserVo();
                BeanUtils.copyProperties(user, vo);
                userIds.add(user.getId());
                list.add(vo);
            }
            Map<Integer, List<Integer>> countMap =
                    crawlerDownloadTaskLogic.findResumeIdListByUserIds(corpCode, userIds);

            for (UserVo user : list) {
                int size = 0;
                if (null != countMap.get(user.getId())) {
                    size = countMap.get(user.getId()).size();
                }
                user.setDownResCount(size);
            }
            result.setData(list);
        }

        return result;
    }

    public Map<String, Object> selectPrimaryKey(String username, String password) {
        try {
            password = EncodeMd.EncodeByMd5(password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> user = userLogic.selectPrimaryKey(username, password);
        if (user != null) {
            if (user.get("token") == null) {
                try {
                    user.put("token", EncodeMd.EncodeByMd5(username + password));
                    User entity = new User();
                    entity.setToken(user.get("token").toString());
                    entity.setId(Integer.valueOf(user.get("id").toString()));
                    userLogic.updateByPrimaryKeySelective(entity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return user;
    }

    public Map<String, Object> selectPrimaryKeyByToken(String token) {
        return userLogic.selectPrimaryKeyByToken(token);
    }

    public Integer selectIsLogin(String token) {
        Map<String, Object> user = userLogic.selectPrimaryKeyByToken(token);
        if (user == null) {
            return -1;
        } else {
            return Integer.valueOf(user.get("id").toString());
        }
    }

    public Map<String, Object> selectPrimaryKeyById(Integer id) {
        return userLogic.selectPrimaryKeyById(id);
    }

    public User findOneById(Integer id) {
        return userLogic.findOneById(id);
    }

    public List<Map<String, Object>> selectUsersByCondition(String realname,
                                                            Integer ishr,
                                                            Integer subcompanyId,
                                                            Integer departmentId) {
        String corpCode = RequestUtils.getCorpCode();

        return userLogic.selectUsersByCondition(realname, ishr, corpCode, departmentId);
    }

    public int insertSelective(User record) {
        return userLogic.insertSelective(record);
    }

    public int updateByPrimaryKeySelective(User record) {
        return userLogic.updateByPrimaryKeySelective(record);
    }


    /**
     * 外部调用接口的方法，返回user当前的token
     *
     * @return
     */
    public String responseToken(String username) {
        Map<String, Object> user = userLogic.selectPrimayByUsername(username);
        String token = "";
        if (user != null) {
            if (user.get("token") == null) {
                try {
                    token = EncodeMd.EncodeByMd5(user.get("username").toString() + user.get("password").toString());
                    User entity = new User();
                    entity.setToken(token);
                    entity.setId((Integer) user.get("id"));
                    userLogic.updateByPrimaryKeySelective(entity);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                token = user.get("token").toString();
            }
        }
        return token;
    }

    public Integer selectPrimaryIdByEmail(String email) {
        return userLogic.selectPrimaryIdByEmail(email);
    }

    public User findAdminUser(String corpCode) {

        return userLogic.findAdminUserByCorp(corpCode);
    }
}
